<?php

class MShippingIn extends MY_Model
{
    const ORDER_ID_DESC = 1;
    const ORDER_ID_ASC = 2;

    const STATUS_DISABLED = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_ONLOAD = 2;
    protected $table = 'shipping_in';

    function __construct()
    {
        parent::__construct();
    }

    public static function getOrderType($orderType = NULL)
    {
        $define = array(
            self::ORDER_ID_DESC => 'ID倒序',
            self::ORDER_ID_ASC => 'ID升序'
        );
        if ($orderType === NULL) {
            return $define;
        } else {
            return key_exists($orderType, $define) ? $define[$orderType] : '';
        }
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATUS_DISABLED => '<label class="label label-danger">作废</label>', self::STATUS_SUCCESS => '<label class="label label-success">成功</label>',self::STATUS_ONLOAD => '<label class="label label-warning">在途</label>');
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }


    public function getList($limit, $sort = false)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        if ($sort) {
            switch ($sort) {
                case self::ORDER_ID_DESC:
                    $this->db->order_by('id', 'desc');
                    break;
                case self::ORDER_ID_ASC:
                    $this->db->order_by('id', 'asc');
                    break;
            }
        } else {
            $this->db->order_by('id', 'desc');
        }

        $query = $this->db->get();
        return $query->result();
    }


    public function getCount()
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getAllProducts(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->product_name.'  (id => '.$v->id.')';
        }
        return $kvArr;
    }

    public function getInList($id_str){
        $this->db->select('*');
        $this->db->from($this->table);

        $this->db->where('id in ('.$id_str.')');
        $query = $this->db->get();
        return $query->result();
    }

    public function getInfoIn($time_begin, $time_end, $type,$shop_id,$city,$group_id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('in_status', 1);
        if ($shop_id){
            $this->db->where('shop_id', $shop_id);
        }

        if ($city){
            $this->db->where('warehouse', $city);
        }

        if ($group_id){
            $this->db->where('group_id', $group_id);
        }

        if ($type == 'init') {
            $format = 'Y-m-d';
            $x = $this->get_weeks();
            $this->db->where('purchase_time <= ', strtotime($x[6] . '23:59:59'));
        } else {
            $begin = strtotime($time_begin);
            $end = strtotime($time_end);
            $days = ($end - $begin) / 86400;
            if ($days > 0 && $days <= 30) {
                $line_type = 3;
                $format = 'Y-m-d';
            } else if ($days > 30 && $days <= 365) {
                $line_type = 2;
                $format = 'Y-m';
            } else {
                $line_type = 1;
                $format = 'Y';
            }
            $x = $this->getDateFromRange($time_begin, $time_end, $line_type);
            if ($line_type == 3) {
                $time_format_end = $time_end . ' 23:59:59';
            } else {
                $time_format_end = $time_end;
            }

            if ($time_end) {
                $this->db->where('purchase_time <= ', strtotime($time_format_end));
            }
        }
        $list = $this->db->get()->result();

        $amount = array();
        if (count($list)) {
            $in_total = 0;
            foreach ($list as $v) {
                $v->purchase_time = date($format, $v->purchase_time);
                $in_total += $v->product_amount;
            }

            foreach ($x as $x_v) {
                $sum = 0;
                $out_amount = $this->MShippingOut->getOutAmountByDate($x_v,$shop_id,$city,$group_id);
                foreach ($list as $key => $value) {
                    if (strtotime($x_v) >= strtotime($value->purchase_time)) {
                        $sum += $value->product_amount;

                    } else {
                        $sum += 0;
                    }
                }
                $sum -= $out_amount;
                $amount[$x_v] = $sum;
            }
        } else {
            foreach ($x as $x_v) {
                $amount[$x_v] = 0;
            }
        }

        $in_out = array();
        $in_out['out'] = $this->MShippingOut->getOutAmountByDate($x[count($x)-1],$shop_id,$city,$group_id);
        $in_out['in']  = $in_total;
        return [
            'success' => 1,
            'msg' => '获取成功',
            'x' => $x,
            'data' => array_values($amount),
            'in_out' => $in_out
        ];
    }
}
